package com.bee.littlebeetravel.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.bee.littlebeetravel.R;
import com.bee.littlebeetravel.config.AppStatus;
import com.bee.littlebeetravel.utils.AppStatusManager;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

public class BaseActivity extends AppCompatActivity {
    private LoadingDialog ld;

    public void showLoading(String loadingMessage) {
        ld = new LoadingDialog(this);
        if (loadingMessage.equals("")) {
            ld.setLoadingText("努力加载中...")
                    .show();
        } else {
            ld.setLoadingText(loadingMessage)
                    .show();
        }
    }

    public void closeLoading() {
        if (ld != null) {
            ld.close();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setStatusBarStyle("1");
        if (AppStatusManager.getInstance().getAppStatus() == AppStatus.STATUS_RECYCLE) {
            //被回收，跳转到启动页面
            Intent intent = new Intent(this, SplashActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }
    }

    /**
     * 设置状态背景和颜色
     * 1.白底黑字
     * 2.黑底白字
     *
     * @param type
     */
    public void setStatusBarStyle(String type) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (type.equals("1")) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            } else if (type.equals("2")) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            }


        }
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            if (type.equals("1")) {
                window.setStatusBarColor(getResources().getColor(R.color.white));
            } else if (type.equals("2")) {
                window.setStatusBarColor(getResources().getColor(R.color.grayf8));
            }
        }
    }

}
